from flask_restful import Resource, reqparse,request
from ....Model.Models import UnSupport as UnUnSupportModel
from ....utils import timestamp,response
from flask_restful_swagger import swagger

# support_poot_parser = reqparse.RequestParser()
# support_poot_parser.add_argument('unsupport_user_id', type=int)
# support_poot_parser.add_argument('information_id', type=int)
# support_poot_parser.add_argument('create_user_id', type=int)

success = 1
notfond = -1
error = 0
'''
点赞表
unsupport_user_id = 点赞者id
information_id = 资讯id
create_user_id = 资讯发布者id
'''
class UnSupportlist(Resource):
    def __init__(self, db, auth=None):
        self.db = db
    def get(self):#获取列表
        try:
            return response(success,None, [each.as_dict() for each in UnSupportModel.query.all()])
        except Exception as er:
            return response(error,er,None)

    def post(self):#添加
        try:
            args = request.form
            self.db.session.add(
                    UnSupportModel(
                        unsupport_user_id=args['unsupport_user_id'],
                        information_id=args['information_id'],
                        create_user_id =args['create_user_id'],
                        update_at=timestamp(),
                        create_at=timestamp()
                    )
                )
            self.db.session.commit
            return response(success,None,None)
        except Exception as er:
            return response(error,er,None)


class UnSupport(Resource):

    def __init__(self, db, auth=None):
        self.db = db

    def get(self,unsupport_id):#查
        try:
            return response(success,None, UnSupportModel.query.filter_by(id=unsupport_id).first().as_dict())
                       
        except Exception as er:
            return response(error,er,None)

    def delete(self,unsupport_id):#删
        try:
            self.db.session.delete(
                UnSupportModel.query.filter_by(id=unsupport_id).first()
            )
            self.db.session.commit()
            return response(success,None,None)
        except Exception as er:
            return response(error,er,None)

    def post(self,unsupport_id):#改
        try:
            support_obj = self.db.session.query.filter_by(id=unsupport_id).first()
            args = request.form
            if(args['unsupport_user_id'] != None):
                support_obj.unsupport_user_id = args['unsupport_user_id']
            if(args['information_id'] != None):
                support_obj.information_id = args['information_id']
            if(args['create_user_id'] != None):
                support_obj.phone = args['create_user_id']
            support_obj.update_at = timestamp()
            self.db.session.commit()
            return response(success,None,None)
        except Exception as er:
            return response(error,er,None)